package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.CallbackBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.CallbackMapper;
import com.java2207.group1.pojo.Callback;
import com.java2207.group1.service.CallbackService;
import com.java2207.group1.vo.callback.BarVo;
import com.java2207.group1.vo.callback.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("callbackService")
public class CallbackServiceImpl implements CallbackService {

    @Autowired
    private CallbackMapper callbackMapper;


    @Override
    public LayuiPageVo<Callback> callbackPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        //mybatis 的分页插件
        PageHelper.startPage(page, limit);

        List<Callback> callbacks = callbackMapper.selectByParams(params);


        //mybatis 的分页数据结构
        PageInfo<Callback> callbackPageInfo = new PageInfo<>(callbacks);

        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中
        LayuiPageVo<Callback> callbackLayuiPageVo = new LayuiPageVo<>();

        callbackLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        callbackLayuiPageVo.setMsg("分页列表数据");   // 提示信息
        callbackLayuiPageVo.setCount(callbackPageInfo.getTotal());     // 总记录数
        callbackLayuiPageVo.setData(callbackPageInfo.getList());    // 分页的列表数据

        return callbackLayuiPageVo;
    }

    @Override
    public int deleteCallback(Integer id) {
        return callbackMapper.deleteCallback(id);
    }

    @Override
    public int addCallback(Callback callback) {
        return callbackMapper.addCallback(callback);
    }

    @Override
    public int updateCallback(Callback callback) {
        return callbackMapper.updateCallback(callback);
    }

    @Override
    public int batchCal(String ids) {
        String[] arr = ids.split(",");
        return callbackMapper.batchCal(arr);
    }

    @Override
    public List<BarVo> setBarData() {
        return callbackMapper.setBarData();
    }

    @Override
    public List<PieVo> setPieData() {
        return callbackMapper.setPieData();
    }

    @Override
    public List<CallbackBo> callbackExcelData(Callback callback) {
        return callbackMapper.callbackExcelData(callback);
    }

}
